package com.btm.easymetadata.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.btm.easymetadata.server.entity.dao.LinkFieldPrdInfo;
import com.btm.easymetadata.server.entity.dto.LinkDTO;
import com.btm.easymetadata.server.mapper.mybatisplus.LinkFieldProdInfoMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@AllArgsConstructor
public class LinkService {

    private LinkFieldProdInfoMapper linkFieldProdInfoMapper;

    public boolean linkTo(LinkDTO linkDTO) {
        LambdaQueryWrapper<LinkFieldPrdInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LinkFieldPrdInfo::getPrdInfoId, linkDTO.getPrdInfoId())
                .eq(LinkFieldPrdInfo::getFieldId, linkDTO.getDataFieldId());
        LinkFieldPrdInfo old = linkFieldProdInfoMapper.selectOne(wrapper);
        if (Objects.isNull(old)) {
            LinkFieldPrdInfo linkFieldPrdInfo = new LinkFieldPrdInfo();
            linkFieldPrdInfo.setFieldId(linkDTO.getDataFieldId());
            linkFieldPrdInfo.setPrdInfoId(linkDTO.getPrdInfoId());
            return 1 == linkFieldProdInfoMapper.insert(linkFieldPrdInfo);
        }
        return true;
    }
}
